﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web;
using HiTeachers.Entities;
using HiTeachers.Services;
using HiTeachers.Utilities;

namespace HiTeachers.Web.SchedulerJobs
{
    public class EmailJob : ISchedulerJob
    {
        public void Execute()
        {
            if (DateTime.Now.Hour != 0) return;
            Logger.Info("Checked to send a notify email to member.");
            RemindFullAccountExpires();
            NotificationtoExpiredUsers();
            Logger.Info("Finish check");
        }

        #region Pricate Methods
        private static void RemindFullAccountExpires()
        {
            IList<Teacher> teachers = TeacherServices.GetRemindMembers();
            if (teachers.Count <= 0) return;

            foreach (Teacher teacher in teachers) SendRemindMemberEmail(teacher);
        }

        private static void NotificationtoExpiredUsers()
        {
            IList<Teacher> teachers = TeacherServices.GetExpiredMembers();

            if (teachers.Count <= 0) return;

            foreach (Teacher teacher in teachers)
            {
                teacher.IsFullAccount = false;
                TeacherServices.SaveOrUpdate(teacher);

                SendNotifyExpiredMemberEmail(teacher);
            }
        }

        private static void SendNotifyExpiredMemberEmail(Teacher teacher)
        {
            string tmpPath = HttpContext.Current.Server.MapPath("/mailtemplates");
            string mailCntPath = Path.Combine(tmpPath, "MailContents.xml");

            var dataSet = new DataSet();
            dataSet.ReadXml(mailCntPath);
            DataRow[] rows = dataSet.Tables["MemberExpired"].Select();
            string subject = Convert.ToString(rows[0]["Subject"]);

            NVelocityBuilder.SetResourcePath(tmpPath);
            var contentMail = NVelocityBuilder.ExpiredEmailBuilder(Convert.ToString(rows[0]["Content"]), teacher.Name, teacher.Email,
                CryptorEngine.Decrypt(teacher.Password, CryptorEngine.EncryptKey, true));

            EmailSender.SendMail(subject, contentMail, teacher.Email);
            Logger.Info("Sent notify expired email to: {0}", teacher.Email);
        }

        private static void SendRemindMemberEmail(Teacher teacher)
        {
            string tmpPath = HttpContext.Current.Server.MapPath("/mailtemplates");
            string mailCntPath = Path.Combine(tmpPath, "MailContents.xml");

            var dataSet = new DataSet();
            dataSet.ReadXml(mailCntPath);
            DataRow[] rows = dataSet.Tables["Remind2Member"].Select();
            string subject = Convert.ToString(rows[0]["Subject"]);

            NVelocityBuilder.SetResourcePath(tmpPath);
            var contentMail = NVelocityBuilder.RemindEmailBuilder(Convert.ToString(rows[0]["Content"]), teacher.Name, teacher.Email,
                CryptorEngine.Decrypt(teacher.Password, CryptorEngine.EncryptKey, true), teacher.ActivatedDate, teacher.DeactivatedDate);

            EmailSender.SendMail(subject, contentMail, teacher.Email);
            Logger.Info("Sent remind expire email to: {0}", teacher.Email);
        }
        #endregion
    }
}
